﻿/***************************************************
//  Copyright (c) Premium Tax Free 2011
***************************************************/

using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Web;
using System.Web.Configuration;
using PremierTaxFree.PTFLib;
using PremierTaxFree.PTFLib.Data;

namespace ReceivingService
{
    public class Global : HttpApplication
    {
        public Global()
        {
            SQLWorker.Default.Error += (s, e) =>
            {
                DataAccess.InsertMessageAsync(e.Exception.Message, Strings.ReceivingWebService, 
                    eMessageTypes.Error, e.Exception.StackTrace, true);
            };
        }

        protected void Application_Start(object sender, EventArgs ev)
        {
            SQLWorker.Default.Start(ThreadPriority.Normal);
            string deploymentPath = WebConfigurationManager.AppSettings[Strings.Transferring_LocalDeploymentPath];

            ReceivingWebService.Table.Add(Strings.Transferring_LocalDeploymentPath, deploymentPath);
            string versionFileName = WebConfigurationManager.AppSettings[Strings.Transferring_VersionFileName];
            ReceivingWebService.Table.Add(Strings.Transferring_VersionFileName, versionFileName);

            string fullFileName = Path.Combine(deploymentPath, versionFileName);
            ReceivingWebService.Table.Add(Strings.Transferring_VersionFileFullFileName, fullFileName);
            string currentVersion = File.ReadAllText(fullFileName);
            ReceivingWebService.Table.Add(Strings.Transferring_CurrentVersion, currentVersion);
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {
            Debugger.Launch();
            Exception ex = HttpContext.Current.Server.GetLastError();
            DataAccess.InsertMessageAsync(ex.Message, Strings.ReceivingWebService, 
                eMessageTypes.Error, ex.StackTrace, true);
        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {
            SQLWorker.Default.Stop();
        }
    }
}